package com.lc.railway.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.app.AppActivity;
import com.lc.railway.http.request.LoginApi;
import com.lc.railway.http.request.MyApi;
import com.lc.railway.http.response.LoginBean;
import com.lc.railway.http.response.MyBean;
import com.lc.railway.manager.InputTextManager;
import com.lc.railway.other.IntentKey;
import com.lc.railway.util.SPUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 登录界面
 */
public final class LoginActivity extends AppActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        InputTextManager.with(this)
                .addView(etPhone)
                .addView(etPassword)
                .setMain(btnLogin)
                .build();
    }

    @OnClick({R.id.btn_login, R.id.btn_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
//
                login();

                break;
            case R.id.btn_register:
                startActivity(RegisterActivity.class);
                break;
        }
    }

    private void login() {
        EasyHttp.post(this)
                .api(new LoginApi().setUsername(etPhone.getText().toString().trim()).setPassword(etPassword.getText().toString().trim())).request(new HttpCallback<LoginBean>(this) {
                    @Override
                    public void onSucceed(LoginBean data) {

                        toast(data.getError_msg());
                        if (data.getError_code()==200){
                            SPUtil.put(getContext(), IntentKey.ID,data.getData());
                            my();

                        }
                    }
                });


    }
    private void my() {
        EasyHttp.post(this)
                .api(new MyApi().setId(SPUtil.get(getContext(), IntentKey.ID,"")))
                .request(new HttpCallback<MyBean>(this) {
                    @Override
                    public void onSucceed(MyBean data) {

                        if (data.getError_code() == 200) {
                            SPUtil.put(getContext(), IntentKey.NAME,data.getData().getCnname());
                            SPUtil.put(getContext(), IntentKey.PHONE,data.getData().getUsername());
                            startActivity(HomeActivity.class);
                            finish();
                        }
                    }
                });
    }
}
